var api = "/api/recruitment/search";
var type;
var position_type = '';
var city = '';
$(function () {
    $(".category-list .category-name-active").eq(0).addClass('category-name-item');
    $(".city-list .category-name-active").eq(0).addClass('category-name-item');

    var title = $("input[name='title']").val();
    console.log(title)
    $('title').text('广联官方网站 招贤纳士 '+title);
    $(".product-nav-title span").text(title);
    $(".tag-item").text("校园招聘");
    if (title === "校招") {
        $(".homepage-child-module img").attr("src","http://localhost:8080/upload/img-MqcDKy-380185.jpg");
        $(".introduction-category-item a").eq(2).addClass('bottom-line');

        type = title;
        var data = {
            "type": title
        }
        pagination(1, data)
    } else if (title === "社招"){
        $(".homepage-child-module img").attr("src","http://localhost:8080/upload/img-Qv2KfJ-118050.jpg");
        $(".introduction-category-item a").eq(3).addClass('bottom-line');
        $(".tag-item").text("社会招聘");
        type = title;
        var data = {
            "type": title
        }
        pagination(1, data)
    }

})

//点击选择类型
$(".category-name-active").click(function () {
    $(this).addClass("category-name-item").parent().siblings().children().removeClass("category-name-item");
    var id = $(this).attr("data-id");
    if (parseInt(id) === 2) {
        position_type = '';
    } else if (parseInt(id) === 3) {
        city = '';
    }
    if (parseInt(id) === 0) {
        position_type = $(this).text()
    } else if (parseInt(id) === 1) {
        city = $(this).text()
    }
    console.log("职位：" + position_type, "城市：" + city)

    var data = {
        "position_type": position_type,
        "city": city,
        "type": type
    }
    pagination(1, data, position_type, city)

})

//enter键盘事件
$('.search').keydown(function (e) {
    if (e.keyCode === 13) {
        var name = $(".search").val();
        console.log(name)
        var data = {
            "position_type": position_type,
            "city": city,
            "name": name,
            "type": type
        }
        pagination(1, data, position_type, city, name)
    }
});
//点击搜索按钮
$('.searchBut').on('click', function (e) {
    var name = $(".search").val();
    var data = {
        "position_type": position_type,
        "city": city,
        "name": name,
        "type": type
    }
    pagination(1, data, position_type, city, name)
})

//渲染模板
function renderTable(data) {
    $("tbody").empty();
    var html = '';
    $.each(data, function (index, item) {
        var curTime = new Date(item.modify_date).format("yyyy-MM-dd");
        html = "<tr class='el-table__row'>\n" +
            "                            <td rowspan='1' colspan='1' class='el-table_1_column_1'>\n" +
            "                                <div class='cell el-tooltip'>" + item.name + "</div>\n" +
            "                            </td>\n" +
            "                            <td rowspan='1' colspan='1' class='el-table_1_column_2 is-center'>\n" +
            "                                <div class=\"cell\">" + item.position_type + "</div>\n" +
            "                            </td>\n" +
            "                            <td rowspan='1' colspan='1' class='el-table_1_column_3 is-center'>\n" +
            "                                <div class='cell el-tooltip'>" + item.city + "</div>\n" +
            "                            </td>\n" +
            "                            <td rowspan='1' colspan='1' class='el-table_1_column_4 is-center'>\n" +
            "                                <div class='cell'>" + curTime + "</div>\n" +
            "                            </td>\n" +
            "                            <td rowspan='1' colspan='1' class='el-table_1_column_5  is-center'>\n" +
            "                                <div class='cell'>\n" +
            "                                    <a href='recruitmentDetails?id=" + item.id + "' target='_self' type='primary' class='apply-but' style='background-image: linear-gradient(-180deg, #1897f2, #0066ff);'>立即申请</a>\n" +
            "                                </div>\n" +
            "                            </td>\n" +
            "                        </tr>";
        $("tbody").append(html);
    })
}


function pagination(currentPage, data, position_type, city, name) {
    postJSON(api, data, function (res) {
        if (res.data.length > 0) {
            $(".pagination-warp").show();
            $(".count-tag span").text(res.count);
            $.jqPaginator("#pagination1", {
                totalPages: Math.ceil(res.count / 10),
                visiblePages: 10,
                currentPage: currentPage,
                first: '<li class="first"><a href="javascript:void(0);">首页</a></li>',
                prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
                next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
                last: '<li class="last"><a href="javascript:void(0);">末页</a></li>',
                page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
                onPageChange: function (page) {
                    // data.offset = page;
                    console.log(page);
                    var data = {
                        "page": page,
                        "position_type": position_type,
                        "city": city,
                        "name": name,
                        "type": type
                    }
                    postJSON(api, data, function (res) {
                        renderTable(res.data);
                    })
                }
            })
        } else {
            $("tbody").empty();
            $(".pagination-warp").hide();
            $("tbody").append("<tr>\n" +
                "                    <th colSpan=\"5\" rowSpan=\"5\"  class=\"el-table_1_column_1 is-leaf\" style='text-align: center;'>\n" +
                "                        <div className=\"cell\">暂无开放职位，敬请期待...</div>\n" +
                "                    </th>\n" +
                "                </tr>")
        }

    })

}